package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.cndragon.R;
import com.sskj.cndragon.bean.AddressBean;
import com.sskj.cndragon.bean.AwardBean;
import com.sskj.cndragon.http.HttpConfig;
import com.sskj.cndragon.presenter.WinningRecordActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.NormalRefreshDataSource;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;

public class WinningRecordActivity extends BaseActivity<WinningRecordActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    private SlimAdapter<AwardBean> slimAdapter;
    private MVCCoolHelper<List<AwardBean>> mvcHelper;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_winning_record;
    }

    @Override
    public WinningRecordActivityPresenter getPresenter() {
        return new WinningRecordActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        toolbar.setLeftButtonOnClickListener(view -> finish());

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 1))
                .setFirstDraw(false)
                .setLastDraw(false)
                .setPaintColor(ContextCompat.getColor(this, R.color.transparent)));
        slimAdapter = SlimAdapter.create().register(R.layout.item_winning, new SlimInjector<AwardBean>() {
            @Override
            public void onInject(AwardBean data, IViewInjector injector, List payloads) {
                injector.image(R.id.iv_picture, HttpConfig.BASE_URL + data.getPrizeImgUrl())
                        .text(R.id.tv_winning_name, data.getPrizeName() + "x" + data.getPrizeNum())
                        .text(R.id.tv_winning_status,
                                data.getPrizeTypeId().equals("5") ?
                                        data.getStatus() == 2 ? "已配送" : "未配送" :
                                        data.getStatus() == 0 ? "已到账" : "已到账")
                        .text(R.id.tv_winning_date, TimeFormatUtil.formatE(data.getCreateTime()));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        NormalRefreshDataSource<List<AwardBean>> normalRefreshDataSource = new NormalRefreshDataSource<List<AwardBean>>(coolRefreshView) {
            @Override
            public Flowable<List<AwardBean>> loadData() {
                return mPresenter.getAwardRecord(SPUtil.get(SPConfig.ID, ""));
            }
        };
        mvcHelper.setDataSource(normalRefreshDataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.setAutoLoadMore(true);
        mvcHelper.setNeedCheckNetwork(false);
        mvcHelper.refresh();

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, WinningRecordActivity.class);
        context.startActivity(intent);
    }
}
